
#include <BFont>
#include <BSizePolicy>

#include "member_BTableItem.h"

using namespace BWE;

member_BTableItem::member_BTableItem(BTableItem* tableItem)
{
	boss = tableItem;
	table = 0;
	ia_form = Form_None;
	ia_align = Align_LeftCenter;
	graph = Graph_None;
	widget = 0;
	row = -1;
	column = -1;
	lineFeed = false;
	selected = false;
	dirty = false;
}
member_BTableItem::~member_BTableItem()
{

}

void member_BTableItem::dirtyCell()
{
	dirty = true;
	if (table)
	{
		table_member(table)->dirty = true;
		table_member(table)->columnHeader.sections[column].dirtyItems = true;
		table_member(table)->rowHeader.sections[row].dirtyItems = true;
		table->fresh();
	}
}
void member_BTableItem::freshSize(const BFont* font)
{
	ia_imageSize = iconSize;
	block.setFont(font);
	block.adjust(0);
	ia_textSize = block.rect.size();
	bool hasImage = icon || image || graph;
	bool showImage = hasImage || (ia_form >= Form_IconOnly && ia_form <= Form_IconUnderText);
	if (ia_form == Form_IconAlone && !hasImage)
		showImage = false;
	if (ia_form == Form_TextAlone && block.text.empty() && hasImage)
		showImage = true;
	if (showImage)
	{
		if (font)
		{
			if (ia_imageSize.width() < 1)
				ia_imageSize.width() = font->size();
			if (ia_imageSize.height() < 1)
				ia_imageSize.height() = font->size();
		}
	}
	else
	{
		ia_imageSize.reset();
	}
	if (widget && widget->visible())
	{
		BSize sizeHint = widget->sizeHint();
		if (ia_imageSize.width() < 1)
			ia_imageSize.width() = sizeHint.width();
		if (ia_imageSize.height() < 1)
			ia_imageSize.height() = sizeHint.height();
	}
	adjust(BRect());
	hintSize = ia_hintSize;
}
void member_BTableItem::adjustRect(int x, int y, int w, int h)
{
	rect.set(x, y, w, h);
	adjust(rect);
	block.rect.setPos(ia_textRect.pos());
	if (widget)
	{
		if (ia_form)
		{
			widget->setVisible(ia_imageRect.valid());
			widget->setRect(ia_imageRect);
			if (ia_align && widget->size() != ia_imageRect.size())
			{
				BRect realRect = ia_imageRect.align(ia_align, widget->size());
				widget->setRect(realRect);
			}
		}
		else
		{
			widget->setVisible(rect.valid());
			widget->setRect(rect);
			if (ia_align && widget->size() != rect.size())
			{
				BRect realRect = rect.align(ia_align, widget->size());
				widget->setRect(realRect);
			}
		}
	}
}

bool member_BTableItem::empty() const
{
	return !widget && block.text.empty();
}
bool member_BTableItem::ignored() const
{
	if (widget)
		return !widget->visible();
	return block.text.empty();
}
